# coding:utf-8
# win.py

import gtk
import account
import const

from auth import auth
account.default_account.printa()

class Win(gtk.Window):
	def __init__(self):
		gtk.Window.__init__(self)
		self.set_title('Rauth')
		self.set_icon_from_file(const.RAUTH_IMAGE)
		self.set_border_width(5)
                self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
		self.connect("delete_event",self.on_close)

		self.__visible=True
		
		self.label_name=gtk.Label("帐号：")
		self.label_pass=gtk.Label("密码：")
		self.label_name.show()
		self.label_pass.show()
		
		self.entry_name=gtk.Entry()
		self.entry_pass=gtk.Entry()
                self.entry_name.set_text(account.default_account.get_username())
                self.entry_pass.set_text(account.default_account.get_password())
		self.entry_pass.set_visibility(False)
		self.entry_name.show()
		self.entry_pass.show()

		self.button_connect=gtk.Button("连接")
		self.button_connect.show()
		
		self.button_disconn=gtk.Button("断开")
		self.button_disconn.set_sensitive(False)
		self.button_disconn.show()

		self.action_region=gtk.HBox()
		self.action_region.pack_start(self.button_disconn,expand=False,fill=False)
		self.action_region.pack_end(self.button_connect,expand=False,fill=False)
		self.action_region.show()

		self.table=gtk.Table(3,2)
		self.table.attach(self.label_name,0,1,0,1)
		self.table.attach(self.label_pass,0,1,1,2)
		self.table.attach(self.entry_name,1,2,0,1)
		self.table.attach(self.entry_pass,1,2,1,2)
		self.table.attach(self.action_region,0,2,2,3)
		self.table.show()
	
		self.main_frame=gtk.Frame("请输入用户信息！")
		self.main_frame.set_shadow_type(gtk.SHADOW_ETCHED_IN)
		self.main_frame.add(self.table)
		self.main_frame.show()

		self.vbox=gtk.VBox()
		self.vbox.pack_start(self.main_frame)
		self.vbox.show()

		self.add(self.vbox)
		self.show_all()
		self.show()

	def get_visible(self):
		return self.__visible

	def set_visible(self,visible=False):
		self.__visible=visible
		if visible :
			self.show()
                        self.move(gtk.WIN_POS_CENTER_ALWAYS)
		else :
			self.hide()

	def set_state(self,conn):
		self.entry_name.set_sensitive(not conn)
		self.entry_pass.set_sensitive(not conn)
		self.button_connect.set_sensitive(not conn)
		self.button_disconn.set_sensitive(conn)
		if conn :
			auth.set_account(self.get_account())
			self.set_title("Rauth 己连接！")
		else    :
			self.set_title("Rauth 己断开！")

	def get_account(self):
		username=self.entry_name.get_text()
		password=self.entry_pass.get_text()
#                account.Account(username,password).printa()
		return account.Account("",username,password)

	def on_close(self,widget,data=None):
		self.set_visible(False)
		return True

	def on_activate(self,widget):
		self.set_visible(not self.get_visible())
		return True

	def quit(self,widget,data=None):
		gtk.main_quit()

win = Win()

if __name__ == '__main__':
	print win
